// Admin.jsx
import memoryUtils from "../../utils/memoryUtils";
import { useEffect, useState } from 'react';
import { useNavigate } from "react-router-dom";
import { Layout } from 'antd';
import LeftNav from "../../components/left-nav";
import Header from "../../components/header";
import Home from "../home/home";
import Category from '../category/category';
import Product from '../product/product';
import ProductHome from "../product/home";
import ProductAddUpdate from "../product/add-update";
import ProductDetail from "../product/detail";

import Role from '../role/role';
import User from '../user/user';
import Bar from '../charts/bar';
import Line from '../charts/line';
import Pie from '../charts/pie';
import { Routes, Route, Navigate } from 'react-router-dom';

const { Footer, Sider, Content } = Layout; // 对象解构

const Admin = () => {
  const navigate = useNavigate();
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    const checkLoginStatus = () => {
      if (!memoryUtils.user?._id) {
        navigate('/login', { replace: true });
      }
      setIsLoading(false);
    };
    checkLoginStatus();
  }, [navigate]);

  if (isLoading) {
    return null;
  }

  return (
    <Layout style={{ minHeight: '100%' }}>
      <Sider>
        <LeftNav />
      </Sider>
      <Layout>
        <Header>Header</Header>
        <Content style={{ margin: 20, backgroundColor: '#fff' }}>
          <Routes>
            <Route path="home" element={<Home />} /> {/* 相对路径，完整路径为 /admin/home */}
            <Route path="category" element={<Category />} />
            {/* <Route path="products/*" element={<Product />} /> */}

            {/* <Route path="products" element={<Product />} /> */}
            <Route path="products" element={<Product />}>
              <Route index element={<ProductHome />} /> {/* 商品列表页 */}
              <Route path="detail" element={<ProductDetail />} /> {/* 详情页 */}
              <Route path="addupdate" element={<ProductAddUpdate />} /> {/* 新增/修改页 */}
            </Route>
            <Route path="user" element={<User />} />
            <Route path="role" element={<Role />} />
            <Route path="charts/bar" element={<Bar />} />
            <Route path="charts/line" element={<Line />} />
            <Route path="charts/pie" element={<Pie />} />
            <Route path="/" element={<Navigate to="/admin/home" />} />
          </Routes>
        </Content>
        <Footer style={{ textAlign: 'center', color: '#cccccc' }}>推荐使用谷歌浏览器，可以获得更佳页面操作体验</Footer>
      </Layout>
    </Layout>
  );
};

export default Admin;